package com.googlecode.antilopebpm.definition;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "process")
public class ProcessDefinition {

	@XmlElementRef
	protected List<ActivityDefinition> activities = new ArrayList<ActivityDefinition>();
	@XmlAttribute(required = true)
	protected String id;
	@XmlAttribute
	protected String name;

	// Properties
	// -----------------------------------------------------------------------
	public List<ActivityDefinition> getActivities() {
		return activities;
	}

	public void setNodes(List<ActivityDefinition> activities) {
		this.activities = activities;
	}

	/**
	 * Gets the value of the id property.
	 * 
	 * @return possible object is {@link String }
	 * 
	 */
	public String getId() {
		return id;
	}

	/**
	 * Sets the value of the id property.
	 * 
	 * @param value
	 *            allowed object is {@link String }
	 * 
	 */
	public void setId(String value) {
		this.id = value;
	}

	/**
	 * Gets the value of the name property.
	 * 
	 * @return possible object is {@link String }
	 * 
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the value of the name property.
	 * 
	 * @param value
	 *            allowed object is {@link String }
	 * 
	 */
	public void setName(String value) {
		this.name = value;
	}

	// Fluent API
	// -------------------------------------------------------------------------
	public ActivityDefinition addNode(ActivityDefinition node) {
		this.activities.add(node);
		return node;
	}

	public List<ActivityDefinition> getStartActivities() {
		List<ActivityDefinition> startActivities = new ArrayList<ActivityDefinition>();

		return startActivities;
	}

	public ActivityDefinition getActivityDefinitionById(
			String activityDefinitionId) {
		ActivityDefinition activityDefinition = null;
		Iterator<ActivityDefinition> it = getActivities().iterator();
		while (it.hasNext()) {
			ActivityDefinition thisActivityDefinition = it.next();
			if (thisActivityDefinition.getId().equals(activityDefinitionId)) {
				activityDefinition = thisActivityDefinition;
			}
		}
		return activityDefinition;
	}

}
